home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / General.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-18  |  16.0 KB  |  513 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Author:  John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30. #include "General.h"
  31. #ifdef WIN32 
  32. #include <gl\glaux.h>
  33. #elif __amigaos__
  34. #include <gl/glaux.h>
  35. #else
  36. #include "aux.h"
  37. #endif
  38.  
  39. #include <malloc.h>
  40.  
  41. void SetDefaults(TestPtr this, InfoItemPtr infoItems)
  42. {
  43.     TypeDependentDataPtr currentEnum;
  44.     int enumValue, intValue;
  45.     GLfloat floatValue;
  46.     char** stringAddress;
  47.     PrintfStringPtr* printfStringAddress;
  48.     GLfloat* floatAddress;
  49.     int* intAddress;
  50.     char* stringValue;
  51.     InfoItemPtr currentItem;
  52.  
  53.     this->infoItems = infoItems;
  54.     for (currentItem = infoItems; currentItem->propName; currentItem++) {
  55.     switch (currentItem->type & ~(NoPrint | NotSettable)) {
  56.         case Enumerated:
  57.         case RangedInteger:
  58.         case UnrangedInteger:
  59.         case RangedHexInteger:
  60.         case UnrangedHexInteger:
  61.         intAddress = (int*)((char*)this + currentItem->offset);
  62.         *intAddress = (int)currentItem->defaultData.intValue;
  63.         break;
  64.         case RangedFloat:
  65.         case UnrangedFloat:
  66.         case RangedFloatOrInt:
  67.         case UnrangedFloatOrInt:
  68.         floatAddress = (GLfloat*)((char*)this + currentItem->offset);
  69.         *floatAddress = (GLfloat)currentItem->defaultData.floatValue;
  70.         break;
  71.         case StringType:
  72.                 stringAddress = (char**)((char*)this + currentItem->offset);
  73.                 *stringAddress = strdup(currentItem->defaultData.stringValue);
  74.         break;
  75.         case PrintfStringType:
  76.                 printfStringAddress = (PrintfStringPtr*)((char*)this + currentItem->offset);
  77.                 *printfStringAddress = new_PrintfString(strdup(currentItem->defaultData.stringValue), 0);
  78.         break;
  79.     }
  80.     }
  81. }
  82.  
  83. /* This function does not free any of the strings in the dst Test.        */
  84. /* You must ensure that you free these strings prior to this, if need be. */ 
  85. void CopyStrings(TestPtr dst, TestPtr src)
  86. {
  87.     InfoItemPtr infoItems = src->infoItems;
  88.     char** dstStringAddress;
  89.     char** srcStringAddress;
  90.     PrintfStringPtr* dstPrintfStringAddress;
  91.     PrintfStringPtr* srcPrintfStringAddress;
  92.     InfoItemPtr currentItem;
  93.  
  94.     for (currentItem = infoItems; currentItem->propName; currentItem++) {
  95.     if ((currentItem->type & ~(NoPrint | NotSettable)) == StringType) {
  96.             dstStringAddress = (char**)((char*)dst + currentItem->offset);
  97.             srcStringAddress = (char**)((char*)src + currentItem->offset);
  98.         if (*srcStringAddress)
  99.                 *dstStringAddress = strdup(*srcStringAddress);
  100.     } else if ((currentItem->type & ~(NoPrint | NotSettable)) == PrintfStringType) {
  101.             dstPrintfStringAddress = (PrintfStringPtr*)((char*)dst + currentItem->offset);
  102.             srcPrintfStringAddress = (PrintfStringPtr*)((char*)src + currentItem->offset);
  103.         if (*srcPrintfStringAddress)
  104.                 *dstPrintfStringAddress = PrintfString__Copy(*srcPrintfStringAddress);
  105.     }
  106.     }
  107. }
  108.  
  109. void FreeStrings(TestPtr this)
  110. {
  111.     InfoItemPtr infoItems = this->infoItems;
  112.     char** stringAddress;
  113.     PrintfStringPtr* printfStringAddress;
  114.     InfoItemPtr currentItem;
  115.  
  116.     for (currentItem = infoItems; currentItem->propName; currentItem++) {
  117.     if ((currentItem->type & ~(NoPrint | NotSettable)) == StringType) {
  118.             stringAddress = (char**)((char*)this + currentItem->offset);
  119.         if (*stringAddress) {
  120.         free(*stringAddress);
  121.         *stringAddress = 0;
  122.         }
  123.     } else if ((currentItem->type & ~(NoPrint | NotSettable)) == PrintfStringType) {
  124.             printfStringAddress = (PrintfStringPtr*)((char*)this + currentItem->offset);
  125.         if (*printfStringAddress) {
  126.         delete_PrintfString(*printfStringAddress);
  127.         *printfStringAddress = 0;
  128.         }
  129.     }
  130.     }
  131. }
  132.  
  133. int Apply(TestPtr this, int propName, AttributePtr attr)
  134. {
  135.     InfoItemPtr infoItems = this->infoItems;
  136.     TypeDependentDataPtr currentEnum;
  137.     int enumValue, intValue;
  138.     GLfloat floatValue;
  139.     char* stringValue;
  140.     PrintfStringPtr printfStringValue;
  141.     int minIntValue, maxIntValue;
  142.     GLfloat minFltValue, maxFltValue;
  143.     InfoItemPtr currentItem;
  144.  
  145.     for (currentItem = infoItems; 
  146.          currentItem->propName && propName != currentItem->propName; 
  147.          currentItem++);
  148.  
  149.     if (currentItem->propName) {
  150.     if (currentItem->type & NotSettable)
  151.         return PropertyNotSettable;
  152.     switch (currentItem->type & ~NoPrint) {
  153.         case Enumerated:
  154.         if (!Attribute__IntOf(attr, &enumValue)) return InvalidValue;
  155.         for (currentEnum = currentItem->typeDependentData;
  156.              (int)currentEnum->value != End && enumValue != (int)currentEnum->value;
  157.              currentEnum++);
  158.         if ((int)currentEnum->value != End) {
  159.             int* dataAddress = (int*)((char*)this + currentItem->offset);
  160.             *dataAddress = enumValue;
  161.             return ApplySuccessful;
  162.         } else {
  163.             return InvalidValue;
  164.         }
  165.         break;
  166.         case RangedInteger:
  167.         case RangedHexInteger:
  168.         if (!Attribute__IntOf(attr, &intValue)) return InvalidValue;
  169.         minIntValue = (int)currentItem->typeDependentData[0].value;
  170.         maxIntValue = (int)currentItem->typeDependentData[1].value;
  171.         if (maxIntValue >= intValue && intValue >= minIntValue) {
  172.             int* dataAddress = (int*)((char*)this + currentItem->offset);
  173.             *dataAddress = intValue;
  174.             return ApplySuccessful;
  175.         } else {
  176.             return InvalidValue;
  177.         }
  178.         break;
  179.         case UnrangedInteger:
  180.         case UnrangedHexInteger:
  181.         if (!Attribute__IntOf(attr, &intValue)) {
  182.             return InvalidValue;
  183.         } else {
  184.             int* dataAddress = (int*)((char*)this + currentItem->offset);
  185.             *dataAddress = intValue;
  186.             return ApplySuccessful;
  187.         }
  188.         break;
  189.         case RangedFloat:
  190.         if (!Attribute__FloatOf(attr, &floatValue)) return InvalidValue;
  191.         minFltValue = currentItem->typeDependentData[0].value;
  192.         maxFltValue = currentItem->typeDependentData[1].value;
  193.         if (maxFltValue >= floatValue && floatValue >= minFltValue) {
  194.             GLfloat* dataAddress = (GLfloat*)((char*)this + currentItem->offset);
  195.             *dataAddress = floatValue;
  196.             return ApplySuccessful;
  197.         } else {
  198.             return InvalidValue;
  199.         }
  200.         break;
  201.         case UnrangedFloat:
  202.         if (!Attribute__FloatOf(attr, &floatValue)) {
  203.             return InvalidValue;
  204.         } else {
  205.             GLfloat* dataAddress = (GLfloat*)((char*)this + currentItem->offset);
  206.             *dataAddress = floatValue;
  207.             return ApplySuccessful;
  208.         }
  209.         break;
  210.         case RangedFloatOrInt:
  211.         if (Attribute__FloatOf(attr, &floatValue)) {
  212.             minFltValue = currentItem->typeDependentData[0].value;
  213.             maxFltValue = currentItem->typeDependentData[1].value;
  214.             if (maxFltValue >= floatValue && floatValue >= minFltValue) {
  215.                 GLfloat* dataAddress = (GLfloat*)((char*)this + currentItem->offset);
  216.                 *dataAddress = floatValue;
  217.                 return ApplySuccessful;
  218.             } else {
  219.                 return InvalidValue;
  220.             }
  221.         } else if (Attribute__IntOf(attr, &intValue)) {
  222.             minIntValue = (int)currentItem->typeDependentData[0].value;
  223.             maxIntValue = (int)currentItem->typeDependentData[1].value;
  224.             if (maxIntValue >= intValue && intValue >= minIntValue) {
  225.                 int* dataAddress = (int*)((char*)this + currentItem->offset);
  226.                 *dataAddress = intValue;
  227.                 return ApplySuccessful;
  228.             } else {
  229.                 return InvalidValue;
  230.             }
  231.         } else {
  232.             return InvalidValue;
  233.         }
  234.         break;
  235.         case UnrangedFloatOrInt:
  236.         if (Attribute__FloatOf(attr, &floatValue)) {
  237.             GLfloat* dataAddress = (GLfloat*)((char*)this + currentItem->offset);
  238.             *dataAddress = floatValue;
  239.             return ApplySuccessful;
  240.         } else if (Attribute__IntOf(attr, &intValue)) {
  241.                     int* dataAddress = (int*)((char*)this + currentItem->offset);
  242.                     *dataAddress = intValue;
  243.                     return ApplySuccessful;
  244.         } else {
  245.             return InvalidValue;
  246.         }
  247.         break;
  248.         case StringType:
  249.         if (!Attribute__StringOf(attr, &stringValue)) {
  250.             return InvalidValue;
  251.         } else {
  252.             char** dataAddress = (char**)((char*)this + currentItem->offset);
  253.                     if (*dataAddress) free(*dataAddress);
  254.                     *dataAddress = strdup(stringValue);
  255.             return ApplySuccessful;
  256.         }
  257.         break;
  258.         case PrintfStringType:
  259.         if (Attribute__StringOf(attr, &stringValue)) {
  260.             PrintfStringPtr* dataAddress = (PrintfStringPtr*)((char*)this + currentItem->offset);
  261.                     if (*dataAddress) delete_PrintfString(*dataAddress);
  262.             *dataAddress = new_PrintfString(strdup(stringValue), 0);
  263.             return ApplySuccessful;
  264.         } else if (Attribute__PrintfStringOf(attr, &printfStringValue)) {
  265.             PrintfStringPtr* dataAddress = (PrintfStringPtr*)((char*)this + currentItem->offset);
  266.                     if (*dataAddress) delete_PrintfString(*dataAddress);
  267.                     *dataAddress = PrintfString__Copy(printfStringValue);
  268.             return ApplySuccessful;
  269.         } else {
  270.             return InvalidValue;
  271.         }
  272.         break;
  273.         default:
  274.         return InvalidValue;
  275.     }
  276.     } else {
  277.     return InvalidProperty;
  278.     }
  279. }
  280.  
  281. #if defined(XWINDOWS)
  282. #include <GL/glx.h>
  283. static int
  284. GetAllVisuals(AttributeListPtr attrList)
  285. {
  286.   extern int xScreen;           /* XXX - better way to get screen ?? */
  287.   Display *dpy;
  288.   int screen;
  289.   XVisualInfo visTemplate, *vis;
  290.   int nVis, useGL, i;
  291.  
  292.   dpy = auxXDisplay();
  293.   if (!dpy) {
  294.     fprintf(stderr, "GetAllVisuals: no display set\n");
  295.     return(0);
  296.   }
  297.  
  298.   screen = xScreen;             /* XXX - better way to get screen ?? */
  299.   
  300.  
  301.   visTemplate.screen = screen;
  302.   vis = XGetVisualInfo(dpy, VisualScreenMask, &visTemplate, &nVis);
  303.  
  304.   for (i = 0; i < nVis; i++) {
  305.     glXGetConfig(dpy, &vis[i], GLX_USE_GL, &useGL);
  306.     if (useGL) {
  307.       AttributeList__AddAttribute(attrList,
  308.                                   new_Attribute_Int((int)vis[i].visualid));
  309.     }
  310.   }
  311.  
  312.   XFree((void *)vis);
  313.   return(1);
  314. }
  315. #elif defined(WIN32)
  316. #include <windows.h>
  317. static int
  318. GetAllVisuals(AttributeListPtr attrList)
  319. {
  320.     HWND hwnd;   
  321.     HDC hdc;    
  322.     PIXELFORMATDESCRIPTOR pfd;    
  323.     int nVis, i;
  324.  
  325.     hwnd = GetActiveWindow();
  326.     if (hwnd == NULL) {
  327.         fprintf(stderr, "GetAllVisuals: could not get a handle to the window\n");
  328.         return(0);
  329.     }
  330.  
  331.     hdc = GetDC(hwnd);
  332.     if (hdc == NULL) {
  333.         fprintf(stderr, "GetAllVisuals: could not get a handle to the DC\n");
  334.         return(0);
  335.     }
  336.  
  337.     nVis = DescribePixelFormat(hdc, 1, sizeof( PIXELFORMATDESCRIPTOR), NULL);
  338.     if (nVis == 0) {
  339.         fprintf(stderr, "GetAllVisuals:  could not get any pixel formats\n");
  340.         return(0);
  341.     }
  342.  
  343.     for (i = 0; i < nVis; i++) {
  344.         DescribePixelFormat(hdc, i, sizeof( PIXELFORMATDESCRIPTOR), &pfd);
  345.         if (pfd.dwFlags && PFD_SUPPORT_OPENGL) {
  346.             AttributeList__AddAttribute(attrList,
  347.                 new_Attribute_Int(i));
  348.         }
  349.     }
  350.     return(1);
  351. }
  352. #elif defined(__OS2__)
  353. #include <os2.h>
  354. static int
  355. GetAllVisuals(AttributeListPtr attrList)
  356. {
  357.     HAB hab = WinQueryAnchorBlock( HWND_DESKTOP);
  358.     PVISUALCONFIG* pvc = pglQueryConfigs( hab);
  359.     PVISUALCONFIG* p;
  360.  
  361.     for ( p = pvc; *p; p++)
  362.     {
  363. printf( "Found visual %d\n", (*pvc)->vid);
  364.     AttributeList__AddAttribute( attrList, new_Attribute_Int(( *pvc)->vid));
  365.     }
  366.  
  367.     free( pvc);
  368. }
  369. #elif defined(__amigaos__)
  370. static int
  371. GetAllVisuals(AttributeListPtr attrList)
  372. {
  373.   return 1;
  374. }
  375. #elif defined(SOME_OTHER_WINDOW_SYSTEM)
  376. #else
  377. #error "unknown window system"
  378. #endif
  379.  
  380. AttributeListPtr GetAllAttrs(TestPtr this, int propName)
  381. {
  382.     AttributeListPtr attrList = new_AttributeList();
  383.     InfoItemPtr infoItems = this->infoItems;
  384.     InfoItemPtr currentItem;
  385.     TypeDependentDataPtr currentEnum;
  386.     int enumValue;
  387.     int i;
  388.     int type;
  389.  
  390.     for (currentItem = infoItems; 
  391.          currentItem->propName && propName != currentItem->propName; 
  392.          currentItem++);
  393.  
  394.     type = currentItem->type & ~(NoPrint | NotSettable);
  395.  
  396.     if (!currentItem->propName) {
  397.         delete_AttributeList(attrList);
  398.         return 0;
  399.     }
  400.  
  401.     if (propName == VisualId) {
  402.         if (GetAllVisuals(attrList)) {
  403.             return attrList;
  404.         } else {
  405.             delete_AttributeList(attrList);
  406.             return 0;
  407.         }
  408.     } else if (type == Enumerated) {
  409.         for (currentEnum = currentItem->typeDependentData;
  410.              (int)currentEnum->value != End;
  411.              currentEnum++) {
  412.             AttributeList__AddAttribute(attrList, new_Attribute_Int((int)currentEnum->value));
  413.         }
  414.         return attrList;
  415.     } else if ((type == RangedInteger || type == RangedHexInteger) &&
  416.                (int)currentItem->typeDependentData[1].value - 
  417.                (int)currentItem->typeDependentData[0].value <= 10) {
  418.         int minIntValue = (int)currentItem->typeDependentData[0].value;
  419.         int maxIntValue = (int)currentItem->typeDependentData[1].value;
  420.         int i;
  421.         for (i=minIntValue; i<=maxIntValue; i++) {
  422.             AttributeList__AddAttribute(attrList, new_Attribute_Int(i));
  423.         }
  424.     return attrList;
  425.     } else {
  426.     delete_AttributeList(attrList);
  427.     return 0;
  428.     }
  429. }
  430.  
  431. /* This section provides support functions for special alignment of memory */
  432.  
  433.   const unsigned long  modulo = 4096;
  434.  
  435. typedef struct _AddressList {
  436.     void* fakeValue;
  437.     void* realValue;
  438.     struct _AddressList *next;
  439. } AddressList, *AddressListPtr;
  440.  
  441. static AddressListPtr addressList = 0;
  442.  
  443. void* AlignMalloc(int size, int modalignment)
  444. {
  445.     void *realAddr, *fakeAddr;
  446.     AddressListPtr newEntry;
  447.  
  448.     realAddr = malloc(size + 2 * modulo);
  449.     CheckMalloc(realAddr);
  450.  
  451.  
  452.     fakeAddr = (void*)((((unsigned long)realAddr + modulo - 1) & (~(modulo-1))) +(unsigned long) modalignment);
  453.  
  454.     /* save this value away so we can free it later */
  455.     newEntry = (AddressListPtr)malloc(sizeof(AddressList));
  456.     CheckMalloc(newEntry);
  457.     newEntry->realValue = realAddr;
  458.     newEntry->fakeValue = fakeAddr;
  459.     newEntry->next = addressList;
  460.     addressList = newEntry;
  461.     return fakeAddr;
  462. }
  463.  
  464. void AlignFree(void* addr)
  465. {
  466.     AddressListPtr list = addressList;
  467.     AddressListPtr prev;
  468.     if (list == 0) {
  469.     printf("GLperf malloc'd memory list empty, you're in BIG trouble!\n");
  470.     exit(1);
  471.     }
  472.     prev = addressList;
  473.     for (list = addressList; list && list->fakeValue != addr; prev = list, list = list->next);
  474.     if (list) {
  475.     free(list->realValue);
  476.     if (addressList == list) {
  477.         addressList = list->next;
  478.     } else {
  479.         prev->next = list->next;
  480.     }
  481.     free(list);
  482.     } else {
  483.     printf("GLperf malloc'd memory list corrupted!\n");
  484.     exit(1);
  485.     }
  486. }
  487.  
  488. void DefineTexImage(int target, int level, int comps, int width, int height, int depth, int extent, int border, int format, int type, void* pixels)
  489. {
  490.     switch (target) {
  491.     case GL_TEXTURE_1D:
  492.         glTexImage1D(target, level, comps, width, border, format, type, pixels);
  493.         break;
  494.     case GL_TEXTURE_2D:
  495. #ifdef GL_SGIS_detail_texture
  496.     case GL_DETAIL_TEXTURE_2D_SGIS:
  497. #endif
  498.         glTexImage2D(target, level, comps, width, height, border, format, type, pixels);
  499.         break;
  500. #ifdef GL_EXT_texture3D
  501.     case GL_TEXTURE_3D_EXT:
  502.         glTexImage3DEXT(target, level, comps, width, height, depth, border, format, type, pixels);
  503.         break;
  504. #endif
  505. #ifdef GL_SGIS_texture4D
  506.     case GL_TEXTURE_4D_SGIS:
  507.         glTexImage4DSGIS(target, level, comps, width, height, depth, extent, border, format, type, pixels);
  508.         break;
  509. #endif
  510.     }
  511. }
  512.  
  513.